package com.wx.kit;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

import com.wx.config.SystemProps;

import net.minidev.json.JSONValue;

public class WxKit {

	/**
	 * @desc ��ȡaccess_token(�÷�����ȡÿ���д�������,ÿ�λ�ȡֵ��ʱ����������Ч)
	 * 
	 * @return
	 */
	public static String getAccessToken() {
		String msg = "";
		try {
			msg = HttpKit.getResponse(HttpKit.httpRequest("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + SystemProps.appID + "&secret=" + SystemProps.appsecret));
			System.out.println(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * @desc �������ںŵĲ˵�
	 * @param accessToken
	 * @param params
	 *            �˵���json�ַ���(��ϸд��������)
	 * @return
	 */
	public static String creatMenu(String accessToken, String params) {
		String msg = "";
		try {
			msg = HttpKit.getResponse(HttpKit.httpRequestPostJson("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken, params.toString()));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	public static String deleteMenu(String accessToken) {
		String msg = "";
		try {
			msg = HttpKit.getResponse(HttpKit.httpRequest("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	public static String addconditional(String accessToken, String params) {
		String msg = "";
		try {
			msg = HttpKit.getResponse(HttpKit.httpRequestPostJson("https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=" + accessToken, params.toString()));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * @desc ��ȡ�û�openId�б�
	 * @param accessToken
	 * @return
	 */
	public static String getUserList(String accessToken) {
		String msg = "";
		try {
			msg = HttpKit.getResponse(HttpKit.httpRequest("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * @desc �����û�openid��ȡ�û���Ϣ
	 */
	public static String getUserDateByOpenId(String accessToken, String openid) {
		String msg = "";
		try {
			msg = HttpKit.getResponse(HttpKit.httpRequest("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openid));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * @desc ��������
	 * @param accessToken
	 * @param params
	 * @return
	 */
	public static String creatUserGroup(String accessToken, String groupName) {
		String msg = "";
		try {
			Map<String, String> groupNames = new HashMap<String, String>();
			groupNames.put("name", groupName);
			Map<String, Map<String, String>> group = new HashMap<String, Map<String, String>>();
			group.put("group", groupNames);
			msg = HttpKit.getResponse(HttpKit.httpRequestPostJson("https://api.weixin.qq.com/cgi-bin/groups/create?access_token=" + accessToken, JSONValue.toJSONString(group)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * @desc ��ȡ�û�����
	 * @param accessToken
	 * @return
	 */
	public static String getUserGroups(String accessToken) {
		String msg = "";
		try {
			msg = HttpKit.getResponse(HttpKit.httpRequest("https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" + accessToken));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * ����������Ϣ����Ӧ���û�
	 * 
	 * @param accessToken
	 * @param openId
	 * @param content
	 * @return
	 */
	public static String sendMessage(String accessToken, String openId, String content) {
		String msg = "";
		try {
			Map<String, String> contentMap = new HashMap<String, String>();
			contentMap.put("content", content);
			Map<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("touser", openId);
			sendMap.put("msgtype", "text");
			sendMap.put("text", contentMap);
			System.out.println(JSONValue.toJSONString(sendMap));
			msg = HttpKit.getResponse(HttpKit.httpRequestPostJson("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken, JSONValue.toJSONString(sendMap)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * oauth2方式进入通过code 获取用户openId以及access_token(网页授权凭证)
	 * 
	 * @param code
	 * @return
	 */
	public static String authorizationCode(String code) {

		String msg = "";
		try {
			msg = HttpKit.getResponse(HttpKit.httpRequest("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + SystemProps.appID + "&secret=" + SystemProps.appsecret + "&code=" + code + "&grant_type=authorization_code"));
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * 通过access_token(网页授权凭证)以及openid获取用户的详细信息，其中包括unionid(只有在公众号获取该公众平台的时候才获取)
	 * 
	 * @param authorizationCode
	 * @param openid
	 * @return
	 */
	public static String getUnionIdByAuthorizationCode(String authorizationCode, String openid) {
		String msg = "";
		try {
			msg = HttpKit.getResponse(HttpKit.httpRequest("https://api.weixin.qq.com/sns/userinfo?access_token=" + authorizationCode + "&openid=" + openid + "&lang=zh_CN"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return msg;
	}

}
